package com.example.amirani.myapplication;

import android.app.Activity;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.RemoteInput;
import android.widget.TextView;
import android.widget.Toast;
import android.app.ProgressDialog;


public class ReplyActivity extends Activity {

    private ProgressDialog progress;
    private ReplyActivity r;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.not_activity);

        r = this;  //getting the instance of the current class

        progress = new ProgressDialog(this);

        Intent intent = getIntent();
        String reply = this.getMessageText(intent).toString(); //get the reply from voice input in the wear

        //TextView text = (TextView)findViewById(R.id.text);
        //text.setText(reply);

        this.openCircularProgress("Paying Bill " + reply);

//        Context context = getApplicationContext();
//        int duration = Toast.LENGTH_SHORT;
//        Toast toast = Toast.makeText(context, reply, duration);
//        toast.show();
    }

    private CharSequence getMessageText(Intent intent) {
        Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
        if (remoteInput != null) {
            return remoteInput.getCharSequence(MyActivity.EXTRA_VOICE_REPLY);
        }
        return null;
    }

    public void openCircularProgress(String message){
        progress.setMessage(message);
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progress.setIndeterminate(true);
        progress.show();
        new android.os.Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                progress.cancel(); //cancel progress after 3sec

                //Send a notification back to wear about bill paid successfully
                NotificationCompat.WearableExtender wearableExtender =
                        new NotificationCompat.WearableExtender()
                                .setHintShowBackgroundOnly(true);

                Notification notification =
                        new NotificationCompat.Builder(r)
                                .setSmallIcon(R.drawable.ic_launcher)
                                .setContentTitle("Success")
                                .setContentText("Airtel Bill Paid. Rs.1,208/- debited from your account")
                                .extend(wearableExtender)
                                .build();
                NotificationManagerCompat notificationManager =
                        NotificationManagerCompat.from(r);

                notificationManager.notify(001, notification); //using same id as first notification so as to cancel the earlier one and showing this new one.

                //Kill / Finish the current Activity
                r.finish();

            }
        }, 3000);
    }
}
